package com.zzr.demo.redis.service.impl;

import com.zzr.demo.redis.service.PreViewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: xiaomi
 * @date: 2021/10/7
 * @description: 实现 String 的预览
 */
@Service
@Slf4j
public class StringPreViewServiceImpl implements PreViewService {

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Override
    public Long getCount(String key) {
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        //STRLEN
        Long size = valueOperations.size(key);
        log.info("size:{}", size);
        return size;
    }

    @Override
    public String getPreView(String key, long endIndex) {
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        //GETRANGE key start end
        String str = valueOperations.get(key, 0, endIndex);
        log.info("preView:{}", str);
        return str;
    }
}
